using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.Globalization;
using System.Web.Security;
using System.Threading;
using BusinessTier.Facade.Implementation;
using WebMVC.Models;

namespace WebMVC.Controllers
{
    public abstract class ControllerBase : Controller
    {

        protected BusinessTier.Facade.Interface.IMembershipService _membershipService;

        public ControllerBase(BusinessTier.Facade.Interface.IMembershipService membershipService)
        {
            _membershipService = membershipService;
        }
        public ControllerBase() : this(new MembershipService())
        {
        }

        protected Culture CurrentCulture
        {
            get
            {
                var name = Thread.CurrentThread.CurrentCulture.Name.Substring(0, 2);
                return (Culture)Enum.Parse(typeof(Culture), name);
            }
        }

        protected Guid CurrentUserId
        {
            get
            {
                if (HttpContext.User.Identity.IsAuthenticated)
                {
                    return new Guid(Membership.GetUser().ProviderUserKey.ToString());
                }
                else
                {
                    return _membershipService.GetAnonymousUserId(HttpContext.Profile.UserName);
                }
            }
        }

        protected string GetLocalizedName(dynamic obj)
        {
            switch (CurrentCulture)
            {
                case Culture.en:
                    return obj.NameEn;
                case Culture.ru:
                    return obj.NameRu;
                case Culture.hy:
                    return obj.NameAm;
            }
            return null;
        }

        protected string GetLocalizedDescription(dynamic obj)
        {
            switch (CurrentCulture)
            {
                case Culture.en:
                    return obj.DescriptionEn;
                case Culture.ru:
                    return obj.DescriptionRu;
                case Culture.hy:
                    return obj.DescriptionAm;
            }
            return null;
        }
    }
    
}
